package services;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.SqlSession;

import play.mvc.Finally;

import com.aton.db.SessionFactory;

import models.Dealer;
import models.mappers.DealerMapper;
import vos.DealerSearchVo;
import vos.DealerVo;

public class DealerService {
	/**
	 * 根据条件统计经销商数量
	 *
	 * @Title: countDealer 
	 * @param vo
	 * @return
	 * @return: long
	 */
	public static long countDealer(DealerSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			DealerMapper mapper = ss.getMapper(DealerMapper.class);
            return mapper.countDealer(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 根据条件查询经销商列表
	 *
	 * @Title: listDealer 
	 * @param vo
	 * @return
	 * @return: List<Dealer>
	 */
	public static List<DealerVo> listDealer(DealerSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			DealerMapper mapper = ss.getMapper(DealerMapper.class);
            return mapper.listDealer(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 批量启用
	 *
	 * @Title: betchEnable 
	 * @param ids 选中记录id
	 * @param operatorid 
	 * @return: void
	 */
	public static Boolean betchEnable(String ids, String operatorid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			String[] id=ids.split(",");
			List<String> listid=new ArrayList<String>();
			Collections.addAll(listid, id);
			Map<String,Object> map=new HashMap<>();
			map.put("listId", listid);
			Date date=new Date();
			map.put("modifytime", date);
			map.put("modifyname", operatorid);
			DealerMapper mapper = ss.getMapper(DealerMapper.class);
            mapper.betchEnable(map);
            ss.commit();
		}catch(Exception e){
			ss.rollback();
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 批量停用
	 *
	 * @Title: betchEnable 
	 * @param ids 选中记录id
	 * @return: void
	 */
	public static Boolean betchDisable(String ids,String operatorid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			String[] id=ids.split(",");
			List<String> listid=new ArrayList<String>();
			Collections.addAll(listid, id);
			Map<String,Object> map=new HashMap<>();
			map.put("listId", listid);
			Date date=new Date();
			map.put("modifytime", date);
			map.put("modifyname", operatorid);
			//停用经销商所有门店
			DealerMapper mapper = ss.getMapper(DealerMapper.class);
			List<String> shopIds= ShopService.selectByDealers(listid);
			ShopService.betchDisable(shopIds, operatorid);
			//停用经销商所有设备
			List<String> deviceIds=DeviceService.selectByDealers(listid);
			DeviceService.betchDisable(deviceIds, operatorid);
            mapper.betchDisable(map);
            ss.commit();
		}catch(Exception e){
			ss.rollback();
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 批量删除
	 *
	 * @Title: betchDel 
	 * @param listid
	 * @return
	 * @return: boolean
	 */
	public static boolean betchDel(List<String> listid) {
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			Map<String,List<String>> map=new HashMap<>();
			map.put("listId", listid);
			DealerMapper mapper = ss.getMapper(DealerMapper.class);
            mapper.betchDel(map);
            ss.commit();
		}catch(Exception e){
			ss.rollback();
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 删除经销商
	 *
	 * @Title: delDealer 
	 * @param id 经销商id
	 * @return
	 * @return: boolean
	 */
	public static boolean delDealer(String id) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			DealerMapper mapper = ss.getMapper(DealerMapper.class);
	        mapper.delDealer(id);
	        ss.commit();
		}
		catch(Exception e){
			ss.rollback();
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		
		return true;
	}
	/**
	 * 验证编号是否存在
	 *
	 * @Title: checkCode 
	 * @param dealerid 经销商编号
	 * @return
	 * @return: boolean
	 */
	public static boolean checkCode(String dealerid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSession();
		try{
			DealerMapper mapper = ss.getMapper(DealerMapper.class);
	        String dealer= mapper.checkCode(dealerid);
	        if(StringUtils.isEmpty(dealer)){
	        	return false;
	        }
	        else{
	        	return true;
	        }
		}catch(Exception e){
			e.printStackTrace();
			return true;
		}finally{
			ss.close();
		}
	}
	/**
	 * 根据id获取经销商信息
	 *
	 * @Title: getById 
	 * @param dealerid 经销商id
	 * @return
	 * @return: Dealer
	 */
	public static DealerVo getById(String dealerid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSession();
		try{
			DealerMapper mapper = ss.getMapper(DealerMapper.class);
			return mapper.getById(dealerid);
		}finally{
			ss.close();
		}
	}
	/**
	 * 保存经销商信息
	 *
	 * @Title: saveDealer 
	 * @param dealer 经销商对象
	 * @return
	 * @return: boolean
	 */
	public static boolean saveDealer(Dealer dealer) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			DealerMapper mapper = ss.getMapper(DealerMapper.class);
			Date date=new Date();
			dealer.createtime=date;
	        mapper.saveDealer(dealer);
	        ss.commit();
		}
		catch(Exception e){
			ss.rollback();
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 更新经销商信息
	 *
	 * @Title: updateDealer 
	 * @param dealer
	 * @return
	 * @return: boolean
	 */
	public static boolean updateDealer(Dealer dealer) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			DealerMapper mapper = ss.getMapper(DealerMapper.class);
			Date date=new Date();
			dealer.modifytime=date;
	        mapper.updateDealer(dealer);
	        ss.commit();
		}
		catch(Exception e){
			ss.rollback();
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 获取下拉列表内容
	 *
	 * @Title: getCombobox 
	 * @return
	 * @return: List<Dealer>
	 */
	public static List<Dealer> getCombobox() {
		SqlSession ss = SessionFactory.getSqlSession();
		try{
			DealerMapper mapper = ss.getMapper(DealerMapper.class);
			return mapper.getCombobox();
		}finally{
			ss.close();
		}
	}
	/**
	 * 导出为excel
	 *
	 * @Title: exportXls 
	 * @param vo
	 * @return
	 * @return: List<DealerVo>
	 */
	public static List<DealerVo> exportXls(DealerSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			DealerMapper mapper = ss.getMapper(DealerMapper.class);
            return mapper.exportXls(vo);
        } finally {
            ss.close();
        }
	}
}
